# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo


client = pymongo.MongoClient(host='localhost', port=27017)
db = client.MeituanComment
collection = db.comments


class MtprojectPipeline(object):
    def process_item(self, item, spider):
        # print("**************this is pipelines.py*****************")
        # print(item)
        comments = []
        tags = []
        for c in item['comment_list']:
            comments.append(c[11:].replace('"', ''))
        for t in item['tag_list']:
            tags.append(t[7:].replace('"', ''))
        comment = {
            'poiId': item['poi_id'],
            'name': item['restaurant_name'],
            'avgScore': item['avg_score'],
            'avgPrize': item['avg_price'],
            'address': item['restaurant_addr'],
            'commentNum': item['comment_num'],
            'comments': comments,
            'tags': tags
        }
        collection.insert_one(comment)

        return item
